home *** CD-ROM | disk | FTP | other *** search
- /* File: Recorder.c
-
- MPW Tool for recording serial line traffic
-
- */
-
-
- #include <Types.h>
- #include <QuickDraw.h>
- #include <Files.h>
- #include <CursorCtl.h>
- #include <Strings.h>
-
- #include <CRMIntf.h>
- #include <CTBUtils.h>
- #include <CMIntf.h>
- #include <FTIntf.h>
- #include <TMIntf.h>
-
- #include <StdIO.h>
- #include <StdLib.h>
-
- pascal void SetupGlob ( void );
- pascal void ReadCompletionGlue1 ( ConnHandle hConn );
- pascal void ReadCompletionGlue2 ( ConnHandle hConn );
- pascal void WriteCompletionGlue1 ( ConnHandle hConn );
- pascal void WriteCompletionGlue2 ( ConnHandle hConn );
-
- #define CommToolBoxTrap 0x8B
- #define UnimplementedTrap 0x9F
- #define Check(err,str) { \
- OSErr errXYZZY; \
- if (( errXYZZY = ( err )) != noErr ) \
- fprintf ( stderr, "Error %d calling %s\n", errXYZZY, str ); \
- else \
- fprintf ( stderr, "%s : noErr\n", str); \
- }
- #define OUTCONFIGSTR "Baud 2400 dataBits 8 Parity None StopBits 1 Port \"Modem Port\"" \
- " Handshake None HoldConnection False RemindDisconnect False"
- #define INCONFIGSTR "Baud 2400 dataBits 8 Parity None StopBits 1 Port \"Printer Port\"" \
- " Handshake None HoldConnection False RemindDisconnect False"
-
- #define BUF_SIZE 1024
- #define FILE_SIZE 5120
-
- short procID1;
- short procID2;
- ConnHandle stream1;
- ConnHandle stream2;
- char buffer1 [ BUF_SIZE ];
- char buffer2 [ BUF_SIZE ];
- long readSize1;
- long writeSize1;
- long readSize2;
- long writeSize2;
- CMFlags inFlags1;
- CMFlags inFlags2;
- short fRefNum;
- short curBuffer;
- short lastBuffer;
- char *nextChar;
- char fBuffer1 [ FILE_SIZE ];
- char fBuffer2 [ FILE_SIZE ];
-
- /* Is the Comm Toolbox actually installed ?? */
- Boolean IsCTBInstalled ( ) {
- return NGetTrapAddress ( UnimplementedTrap, OSTrap ) !=
- NGetTrapAddress ( CommToolBoxTrap, OSTrap );
- }
-
- short InitAll ( void ) {
- OSErr err;
-
- InitGraf ( &qd.thePort );
- /*
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs ( NULL );
- InitCursor ();
- */
-
- InitCursorCtl ( NULL );
- SetupGlob ();
-
- if ( !IsCTBInstalled ) {
- fprintf ( stderr, "Comm Toolbox not installed!\n" );
- return 1;
- }
-
- /* Load up the Communications Toolbox */
- (void) InitCTBUtilities ();
- (void) InitCRM ();
-
- err = InitTM ();
- if ( err == tmNoTools ) {
- fprintf ( stderr, "No terminal tools found\n" );
- return 2;
- }
-
- err = InitCM ();
- if ( err == cmNoTools ) {
- fprintf ( stderr, "No connection tools found\n" );
- return 2;
- }
-
- err = InitFT ();
- if ( err == ftNoTools ) {
- fprintf ( stderr, "No file transfer tools found\n" );
- return 2;
- }
-
- return 0;
- }
-
-
- void ExitProc ( void ) {
- long numBytes;
- char *theBuffer;
-
-
- theBuffer = curBuffer == 1 ? fBuffer1 : fBuffer2;
- numBytes = nextChar - theBuffer;
- if ( numBytes > 0 ) {
- Check ( FSWrite ( fRefNum, &numBytes, theBuffer ), "Write - AtExit" );
- }
-
- Check ( FSClose ( fRefNum ), "FSClose" );
- DebugStr ( "\pBefore KillIO" );
-
- Check ( CMIOKill (stream1, cmDataIn), "CMIOKill - input");
- Check ( CMIOKill (stream2, cmDataIn), "CMIOKill - output");
-
- /* Close the connection and dispose of the connection record */
- if ( stream1 != NULL ) {
- Check ( CMClose ( stream1, false, NULL, 0, true ), "CMCLose - input" );
- CMDispose ( stream1 );
- }
-
- if ( stream2 != NULL ) {
- Check ( CMClose ( stream2, false, NULL, 0, true ), "CMCLose - output" );
- CMDispose ( stream2 );
- }
- }
-
- ConnHandle InitStream ( short *procID ) {
- CMBufferSizes bSize;
-
- /* Open a connection tool */
- bSize [ cmDataIn ] = BUF_SIZE; bSize [ cmDataOut ] = BUF_SIZE;
- bSize [ cmCntlIn ] = 0; bSize [ cmCntlOut ] = 0;
- bSize [ cmAttnIn ] = 0; bSize [ cmAttnOut ] = 0;
- bSize [ cmRsrvIn ] = 0; bSize [ cmRsrvOut ] = 0;
- *procID = CMGetProcID ( "\pSerial" );
- return CMNew ( *procID, cmQuiet + cmNoMenus, bSize, 0L, 0L );
- }
-
-
- /* Called at interrupt level */
- pascal void XferBytes ( long sz, char *buffer, char dir ) {
-
- while ( sz-- > 0 ) {
- *nextChar++ = dir;
- *nextChar++ = *buffer++;
- if ( curBuffer == 1 ) {
- if ( nextChar - fBuffer1 == FILE_SIZE ) {
- nextChar = fBuffer2;
- curBuffer = 2;
- }
- }
- else {
- if ( nextChar - fBuffer2 == FILE_SIZE ) {
- nextChar = fBuffer1;
- curBuffer = 1;
- }
- }
- }
- }
-
- /* Interrupt routine */
- pascal void ReadCompletion1 ( ConnHandle hConn ) {
-
- if ((*hConn)->errCode == noErr ) {
- writeSize1 = (*hConn)->asyncCount [ cmDataIn ];
- XferBytes ( writeSize1, buffer1, '1' );
- CMWrite ( stream2, buffer1, &writeSize1,
- cmData, true, (ProcPtr) WriteCompletionGlue1, 0, inFlags1 );
- }
- }
-
-
- /* Interrupt routine */
- pascal void WriteCompletion1 ( ConnHandle hConn ) {
-
- /* re-enable the read */
- if ((*hConn)->errCode == noErr ) {
- readSize1 = 1;
- CMRead ( stream1, buffer1, &readSize1,
- cmData, true, (ProcPtr) ReadCompletionGlue1, 0, &inFlags1 );
- }
- }
-
-
- /* Interrupt routine */
- pascal void ReadCompletion2 ( ConnHandle hConn ) {
-
- if ((*hConn)->errCode == noErr ) {
- writeSize2 = (*hConn)->asyncCount [ cmDataIn ];
- XferBytes ( writeSize2, buffer2, '2' );
- CMWrite ( stream1, buffer2, &writeSize2,
- cmData, true, (ProcPtr) WriteCompletionGlue2, 0, inFlags2 );
- }
- }
-
-
- /* Interrupt routine */
- pascal void WriteCompletion2 ( ConnHandle hConn ) {
-
- /* re-enable the read */
- if ((*hConn)->errCode == noErr ) {
- readSize2 = 1;
- CMRead ( stream2, buffer2, &readSize2,
- cmData, true, (ProcPtr) ReadCompletionGlue2, 0, &inFlags2 );
- }
- }
-
-
- int main ( int argc, char *argv[] ) {
- short err;
- long numBytes, cnt;
- char *theBuffer;
-
- if ( err = InitAll ( ) != 0 )
- exit ( err );
-
- if ( argc != 2 ) {
- fprintf ( stderr, "Usage: %s <fileName>\n", argv [ 0 ] );
- exit ( 1 );
- }
-
- c2pstr ( argv [ 1 ] );
- curBuffer = lastBuffer = 1;
- nextChar = fBuffer1;
- Check ( Create ( argv [ 1 ], 0, 'MPS ', 'TEXT' ), "Create" );
- Check ( FSOpen ( argv [ 1 ], 0, &fRefNum ), "Open" );
- Check ( SetFPos ( fRefNum, fsFromStart, 0L ), "SetFPos" );
- atexit ( ExitProc );
-
- /* Open a connection tool */
- stream1 = InitStream ( &procID1 );
- if ( stream1 == NULL ) {
- fprintf ( stderr, "Cannot create input handle\n" );
- return 3;
- }
-
- /* Open another connection tool */
- stream2 = InitStream ( &procID1 );
- if ( stream2 == NULL ) {
- fprintf ( stderr, "Cannot create output handle\n" );
- return 3;
- }
-
- /* Configure the connection */
- // DebugStr ( "\pConfig" );
- Check ( CMSetConfig ( stream1, INCONFIGSTR ), "CMSetConfig - In" );
- Check ( CMOpen ( stream1, false, NULL, -1 ), "CMOpen - In" );
- Check ( CMListen ( stream1, false, NULL, -1 ), "CMListen - In" );
-
- Check ( CMSetConfig ( stream2, OUTCONFIGSTR ), "CMSetConfig - Out" );
- Check ( CMOpen ( stream2, false, NULL, -1 ), "CMOpen - Out" );
- Check ( CMListen ( stream2, false, NULL, -1 ), "CMListen - Out" );
-
- /* Do an ansych read on the input stream */
- readSize1 = 1;
- Check ( CMRead ( stream1, buffer1, &readSize1,
- cmData, true, (ProcPtr) ReadCompletionGlue1, 0, &inFlags1 ), "CMRead" );
- readSize2 = 1;
- Check ( CMRead ( stream2, buffer2, &readSize2,
- cmData, true, (ProcPtr) ReadCompletionGlue2, 0, &inFlags2 ), "CMRead" );
- cnt = 0;
- while ( true ) {
-
- /* write out any buffered data */
- if ( lastBuffer != curBuffer ) {
- fprintf ( stderr, "Writing File\n" );
- numBytes = FILE_SIZE;
- theBuffer = lastBuffer == 1 ? fBuffer1 : fBuffer2;
- Check ( FSWrite ( fRefNum, &numBytes, theBuffer ), "FSWrite" );
- lastBuffer = curBuffer;
- }
-
- cnt++;
- if ( cnt % 256 == 0 ) {
- CMIdle ( stream1 );
- CMIdle ( stream2 );
- SpinCursor ( 1 );
- }
-
- }
-
- return 0;
- }
-